package com.zjitc.model.intelligent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Properties;

/**
 * Create By IntelliJ IDEA
 *
 * @author:lhl
 * @create-Time:2017/12/12 8:13
 * @description:
 */
public class ChartContent {

    public static void main(String[] args) throws IOException {
        String question = "给我唱一首刘德华的忘情水";
        ChartContent chart = new ChartContent();
        chart.connection(question);
    }

    public String connection(String question) throws IOException {
        /**
         * 读取配置文件
         */
        String apikey = getKeyId();
        /**
         * 以上是读取配置文件
         */
        String userid = "12321-s";
        String info = URLEncoder.encode(question, "utf-8");
        String getURL =
                "http://www.tuling123.com/openapi/api?key=" + apikey + "&info=" + info + "&userid" + userid;
        URL getUrl = new URL(getURL);
        HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
        connection.connect();

        // 取得输入流，并使用Reader读取
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(connection.getInputStream(), "utf-8"));
        StringBuffer sb = new StringBuffer();
        String line = "";
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        reader.close();
        // 断开连接
        connection.disconnect();
        System.out.println(sb);
        return String.valueOf(sb);
    }

    private String getKeyId() throws IOException {
        InputStream stream = ChartContent.class.getClassLoader().getResourceAsStream("key.properties");
        Properties properties = new Properties();
        properties.load(stream);
        return properties.getProperty("apiKey");
    }
}
